//
//  MaterialChargeViewController.m
//  SsehMaster
//
//  Created by 杨阳 on 2018/8/12.
//  Copyright © 2018年 yinlong. All rights reserved.
//

#import "MaterialChargeViewController.h"
#import "MaterialViewController.h"      //材料检索
#import "MaterialChargeTableViewCell.h"
#import "SelectdMateralModel.h"         //需要提交的材料
#import "SearchMaterialModel.h"         //检索选择的材料

@interface MaterialChargeViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

@end

static NSString * const MaterialChargeTableViewCellID = @"MaterialChargeTableViewCellID";

@implementation MaterialChargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self layoutUI];
}

-(void)layoutUI{
    
    self.ssehTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, VIEW_HEIGHT-NAVIGATION_BAR_HEIGHT-50-80) style:UITableViewStylePlain];
    self.ssehTableView.delegate = self;
    self.ssehTableView.dataSource = self;
    self.ssehTableView.backgroundColor = [UIColor backGroudColorForView];
    self.ssehTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.ssehTableView.showsVerticalScrollIndicator = NO;
    self.ssehTableView.showsHorizontalScrollIndicator = NO;
    [self.ssehTableView registerNib:[UINib nibWithNibName:@"MaterialChargeTableViewCell" bundle:nil] forCellReuseIdentifier:MaterialChargeTableViewCellID];
    [self.view addSubview:self.ssehTableView];
    
    //尾部视图
    self.ssehTableView.tableFooterView = self.footerView;
    
}

-(UIView *)footerView{
    
    if (_footerView == nil) {
        __weak typeof (self)weakSelf = self;
        _footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, VIEW_WIDTH, 120)];
        _footerView.backgroundColor = [UIColor backGroudColorForView];
        
        //选择项
        UIView * selectBgView = [UIView new];
        selectBgView.backgroundColor = [UIColor whiteColor];
        [_footerView addSubview:selectBgView];
        [selectBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf.footerView);
            make.top.equalTo(weakSelf.footerView).offset(10);
            make.height.mas_equalTo(50);
        }];
        
        UIView * lineView = [UIView new];
        lineView.backgroundColor = [UIColor blackColor];
        [selectBgView addSubview:lineView];
        [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(selectBgView.mas_top).offset(10);
            make.bottom.equalTo(selectBgView.mas_bottom).offset(-10);
            make.left.equalTo(selectBgView.mas_left).offset(VIEW_WIDTH/3-0.5);
            make.width.mas_equalTo(1);
        }];
        
        UIView * lineView1 = [UIView new];
        lineView1.backgroundColor = [UIColor blackColor];
        [selectBgView addSubview:lineView1];
        [lineView1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(selectBgView.mas_top).offset(10);
            make.bottom.equalTo(selectBgView.mas_bottom).offset(-10);
            make.right.equalTo(selectBgView.mas_right).offset(-(VIEW_WIDTH/3-0.5));
            make.width.mas_equalTo(1);
        }];
        
        //选择材料
        
        self.materialsNameLabel = [UILabel new];
        self.materialsNameLabel.text = @"请选择材料";
        self.materialsNameLabel.textColor = [UIColor textLightGrayColor];
        self.materialsNameLabel.font = [UIFont systemFontOfSize:16];
        self.materialsNameLabel.textAlignment = NSTextAlignmentCenter;
        self.materialsNameLabel.userInteractionEnabled = YES;
        [selectBgView addSubview:self.materialsNameLabel];
        [self.materialsNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.bottom.equalTo(selectBgView);
            make.right.equalTo(lineView.mas_left);
        }];
        
        UITapGestureRecognizer * selectMaterialsTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(materialsClick:)];
        [self.materialsNameLabel addGestureRecognizer:selectMaterialsTap];
        
        //输入数量
        self.numTf = [[UITextField alloc]initWithFrame:CGRectMake(VIEW_WIDTH/3+0.5, 0, VIEW_WIDTH/3-1, 50)];
        self.numTf.placeholder = @"请输入数量";
        self.numTf.textAlignment = NSTextAlignmentCenter;
        self.numTf.textColor = [UIColor textLightGrayColor];
        self.numTf.font = [UIFont systemFontOfSize:16];
        self.numTf.keyboardType = UIKeyboardTypeDecimalPad;
        self.numTf.delegate = self;
        [selectBgView addSubview:self.numTf];
        
        //输入价格
        self.priceTf = [[UITextField alloc]initWithFrame:CGRectMake((VIEW_WIDTH/3)*2+0.5, 0, VIEW_WIDTH/3-1, 50)];
        self.priceTf.placeholder = @"请输入单价";
        self.priceTf.textAlignment = NSTextAlignmentCenter;
        self.priceTf.textColor = [UIColor textLightGrayColor];
        self.priceTf.font = [UIFont systemFontOfSize:16];
        self.priceTf.keyboardType = UIKeyboardTypeDecimalPad;
        [selectBgView addSubview:self.priceTf];
        
        //继续添加
        
        UIButton * goonAddButton = [UIButton buttonWithType:UIButtonTypeCustom];
        goonAddButton.titleLabel.font = [UIFont systemFontOfSize:16];
        goonAddButton.backgroundColor = [UIColor whiteColor];
        goonAddButton.layer.cornerRadius = 5;
        [goonAddButton setTitle:@"继续添加" forState:UIControlStateNormal];
        [goonAddButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [goonAddButton addTarget:self action:@selector(goonAddButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [_footerView addSubview:goonAddButton];
        [goonAddButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.left.right.equalTo(weakSelf.footerView);
            make.height.mas_equalTo(50);
            
        }];
    }
    return _footerView;
}

-(void)materialsClick:(UITapGestureRecognizer *)tap{
    
    __weak typeof (self)weakSelf = self;
    MaterialViewController * vc = [[MaterialViewController alloc]init];
    vc.type = SelectdMateralTpyeNew;
    vc.complentSelect = ^(MaterialInfoModel *model,NSIndexPath * index) {
        weakSelf.selectMaterialModel = model;
        weakSelf.materialsNameLabel.text = model.material_name;
    };
    [self.navigationController pushViewController:vc animated:YES];
    
}

-(void)goonAddButtonClick:(UIButton *)button{
    
    if (self.selectMaterialModel == nil) {
        [self showTipWithTitle:@"请选择材料"];
        return;
    }
    
    if (kStringIsEmpty(self.numTf.text)) {
        [self showTipWithTitle:@"请输入材料数量"];
        return;
    }
    
    if ([self.numTf.text floatValue]<=0) {
        [self showTipWithTitle:@"请输入大于0的的材料数量"];
        return;
    }
    
    if (kStringIsEmpty(self.priceTf.text)) {
        [self showTipWithTitle:@"请输入材料价格"];
        return;
    }
    
    SelectdMateralModel * model = [[SelectdMateralModel alloc]init];
    model.material_id = self.selectMaterialModel.material_id;
    model.order_material_id = self.selectMaterialModel.order_material_id;
    model.material_name = self.selectMaterialModel.material_name;
    model.material_count = self.numTf.text;
    model.material_price = self.priceTf.text;
    [self.chargeDataSourcel addObject:model];
    [self.ssehTableView reloadData];
    self.selectMaterialModel = nil;
    self.materialsNameLabel.text = @"请选择材料";
    self.numTf.text = nil;
    self.priceTf.text = nil;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.chargeDataSourcel.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 50;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    MaterialChargeTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:MaterialChargeTableViewCellID];
    cell.midTF.delegate = self;
    SelectdMateralModel * model = self.chargeDataSourcel[indexPath.row];
    [cell configData:model andIndex:indexPath];
    
    __weak typeof (self)weakSelf = self;
    
    cell.leftLabelComplete = ^(NSIndexPath *index) {
        
        MaterialViewController * vc = [[MaterialViewController alloc]init];
        vc.type = SelectdMateralTpyeOld;
        vc.lastIndexPath = indexPath;
        vc.complentSelect = ^(MaterialInfoModel *model,NSIndexPath * index) {
            SelectdMateralModel * oldModel = weakSelf.chargeDataSourcel[index.row];
            oldModel.material_name = model.material_name;
            oldModel.material_id = model.material_id;
            oldModel.order_material_id = model.order_material_id;
            if (index != nil) {
                [weakSelf.ssehTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
            }
            };
            
        [self.navigationController pushViewController:vc animated:YES];
        
    };
    
    cell.midTFComplete = ^(NSString *text, NSIndexPath *index) {
        
        SelectdMateralModel * oldModel = weakSelf.chargeDataSourcel[index.row];
        oldModel.material_count = text;
        [weakSelf.ssehTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
    };
    
    cell.rightTFComplete = ^(NSString *text, NSIndexPath *index) {
        SelectdMateralModel * oldModel = weakSelf.chargeDataSourcel[index.row];
        oldModel.material_price = text;
        [weakSelf.ssehTableView reloadRowsAtIndexPaths:@[index] withRowAnimation:UITableViewRowAnimationFade];
    };
    
    return cell;
    
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (kStringIsEmpty(string)) {
        return YES;
    }
    if (kStringIsEmpty(textField.text) && [string isEqualToString:@"."]) {
        return NO;
    }
    if ([string isEqualToString:@"."] && [textField.text containsString:@"."]) {
        return NO;
    }
    return [@"0123456789." containsString:string];
    
}

-(NSMutableArray *)chargeDataSourcel{
    
    if (_chargeDataSourcel == nil) {
        _chargeDataSourcel = [[NSMutableArray alloc]initWithCapacity:0];
    }
    return _chargeDataSourcel;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
